<template>
    <view class="merOrder wanl-order-list">
        <view
            class="navbar"
            style="width: 100%; position: fixed; top: 100; left: 0; background-color: #f7f7f7; z-index: 10"
        >
            <view
                class="nav-item text-df"
                v-for="(item, index) in tabList"
                :key="item.status"
                :class="{ current: tabCurrentIndex === index }"
                @tap="tabClick(index)"
            >
                {{ item.name }}
            </view>
        </view>
        <view class="wanl-list" style="padding-top: 60rpx">
            <!-- 导航条 -->
            <view class="head" :class="{ headtop: scrollStype }">
                <view class="cue" style="z-index: 10 !important">
                    <view class="bar" style="justify-content: space-around">
                        <view class="item" :class="{ current: tabCurrentIndex2 === 0 }" @tap="tabClick2(0)">全部</view>
                        <view class="item" :class="{ current: tabCurrentIndex2 === 1 }" @tap="tabClick2(1)"
                            >待发货</view
                        >
                        <view class="item" :class="{ current: tabCurrentIndex2 === 2 }" @tap="tabClick2(2)"
                            >待收货</view
                        >
                    </view>
                </view>
            </view>
            <!-- 主体 -->
            <block v-if="goodsData.length > 0">
                <wanl-product2
                    @onWriteOff="onWriteOff"
                    @closeError="closeError"
                    @confirmFaHuo="confirmFaHuo"
                    :dataList="goodsData"
                    :dataType="goodsType"
                />
            </block>
            <uni-load-more :status="status" :content-text="contentText" />
            <view class="edgeInsetBottom"></view>
        </view>

        <uv-tabbar :value="currentTab" @change="changeTab">
            <uv-tabbar-item :text="item.text" v-for="(item, index) in tabsList" :key="index">
                <!-- <template v-slot:active-icon>
                    <image class="icon" :src="item.iconPath"></image>
                </template>
<template v-slot:inactive-icon>
                    <image class="icon" :src="item.selectedIconPath"></image>
                </template> -->
            </uv-tabbar-item>
        </uv-tabbar>
        <wanl-empty v-if="goodsData.length === 0" src="collect_default3x" text="没有找到任何商品" />
        <!-- 弹出层 积分-->
        <uni-popup ref="popup" type="center" background-color="#fff">
            <view style="padding: 20rpx; background-color: #fff; border-radius: 20rpx">
                <text style="font-size: 40rpx; font-weight: bold; text-align: center; display: block; padding: 20rpx"
                    >请输入快递信息</text
                >
                <view class="cu-form-group">
                    <view class="title">快递公司</view>
                    <input @tap="openGongSi" v-model="express_name" placeholder="请输入快递公司" />
                    <uv-picker ref="picker" :columns="expressList" keyName="name" @confirm="confirm"></uv-picker>
                </view>
                <view class="cu-form-group">
                    <view class="title">快递单号</view>
                    <input v-model="express.express_sn" placeholder="请输入快递单号" />
                </view>
                <view class="cu-form-group">
                    <button class="cu-btn bg-red margin-tb-sm lg" @tap="closeError">取消</button>
                    <button class="cu-btn bg-gradual-orange margin-tb-sm lg" @tap="confirmFaHuo">确定</button>
                </view>
            </view>
        </uni-popup>
    </view>
</template>
<script>
import wanlProduct2 from "@/components/wanl-product2/wanl-product2";
export default {
    name: "merOrder",
    components: {
        wanlProduct2,
    },
    mixins: [],
    props: {},
    data() {
        return {
            imgUrl: this.$imgUrl,

            tabCurrentIndex: 0, // 当前tab的index
            tabList: [
                {
                    name: "积分订单",
                },
                {
                    name: "商城订单",
                },
            ],
            tabCurrentIndex2: 0, // 当前tab的index
            currentTab: 1, // 当前选中的tabbar
            tabsList: [
                // 0商品 1订单 2我的
                {
                    // iconPath: imgUrl + "/static/wdcp-fl.png",
                    // selectedIconPath: imgUrl + "/static/wdcp-fl-active.png",
                    text: "商品",
                },
                {
                    // iconPath: imgUrl + "/static/wdcp-wd.png",
                    // selectedIconPath: imgUrl + "/static/wdcp-wd-active.png",
                    text: "订单",
                },
                {
                    // iconPath: imgUrl + "/static/wdcp-wd.png",
                    // selectedIconPath: imgUrl + "/static/wdcp-wd-active.png",
                    text: "我的",
                },
            ],
            goodsData: [], //积分商品数据
            WanlScroll: 0, //记录页面滚动
            scrollStype: false, //滚动状态
            goodsType: "score",
            empty: "没有找到任何宝贝",
            reload: false, //判断是否上拉
            last_page: 0,
            status: "loading",
            contentText: {
                contentdown: "",
                contentrefresh: "正在加载..",
                contentnomore: "没有更多数据了",
            },
            params: {
                page: 1,
            },
            filterIndex: 0, //
            express: {
                //积分
                order_id: 0, //订单id
                express_sn: "", //快递单号
                express_company: "", //快递公司
            },
            expressList: [], //快递公司
            express_name: "", //快递公司选中的值
            type: "", //score:积分订单，goods:商城订单--发货
        };
    },
    computed: {},
    watch: {},
    mounted() {},
    // 监听上拉
    onPullDownRefresh() {
        this.params.page = 1;
        this.goodsData = [];
        this.reload = true;
        if (this.tabCurrentIndex === 0) {
            this.getScoreList(this.tabCurrentIndex2);
        } else {
            this.getShopList(this.tabCurrentIndex2);
        }
    },
    // 下拉刷新
    onReachBottom() {
        if (this.params.page >= this.last_page) {
            this.status = "noMore";
        } else {
            this.reload = false;
            this.contentText.contentdown = "上拉显示更多";
            this.params.page = this.params.page + 1; //页码+1
            this.status = "loading";
            if (this.tabCurrentIndex === 0) {
                this.getScoreList();
            } else {
                this.getShopList();
            }
        }
    },
    // 监听滚动
    onPageScroll(object) {
        if (object.scrollTop > this.WanlScroll) {
            this.scrollStype = false;
        } else {
            this.scrollStype = true;
        }
        if (object.scrollTop < 0) {
            this.scrollStype = false;
        }
        this.WanlScroll = object.scrollTop;
    },
    onLoad(options) {
        this.getScoreList();
    },
    methods: {
        // 确认发货
        confirmFaHuo() {
            uni.showModal({
                title: "提示",
                content: "是否确认发货?",
                showCancel: true,
                success: async ({ confirm, cancel }) => {
                    if (confirm) {
                        if (this.type == "score") {
                            await uni.request({
                                url: "/shop.score/DeliverOrder",
                                data: this.express,
                                success: (res) => {
                                    uni.showToast({
                                        title: "发货成功",
                                        icon: "none",
                                    });
                                    this.getScoreList();
                                    this.$refs.popup.close();
                                },
                                fail: (err) => {},
                                complete: (res) => {
                                    if (res.res.code == 0) {
                                        this.$refs.popup.close();
                                    }
                                },
                            });
                        } else if (this.type == "goods") {
                            await uni.request({
                                url: "/shop.shop/ShopOrderSend",
                                data: {
                                    order_id: this.express.order_id,
                                    express_no: this.express.express_sn,
                                    express_name: this.express_name,
                                },
                                success: (res) => {
                                    uni.showToast({
                                        title: "发货成功",
                                        icon: "none",
                                    });
                                    this.$refs.popup.close();
                                    this.getShopList();
                                },
                                fail: (res) => {},
                                complete: (res) => {
                                    if (res.res.code == 0) {
                                        this.$refs.popup.close();
                                    }
                                },
                            });
                        }
                    }
                },
            });
        },
        //选择快递公司
        openGongSi() {
            this.getExpress();
            this.$refs.picker.open();
        },
        //确认选择快递公司
        confirm(e) {
            this.express_name = e.value[0].name;
            this.express.express_company = e.value[0].code;
        },
        //去发货
        onWriteOff(order_id, type) {
            this.express.order_id = order_id;
            this.type = type;
            this.$refs.popup.open("center");
        },
        //关闭弹出层
        closeError() {
            this.$refs.popup.close();
        },
        //获取快递公司
        async getExpress() {
            await uni.request({
                url: "/shop.shop/GetKuaiDiList",
                method: "POST",
                success: (res) => {
                    this.expressList = [res.res.data];
                },
            });
        },
        //获取积分订单列表
        async getScoreList(type) {
            await uni.request({
                url: "/shop.score/GetScoreOrderList",
                method: "POST",
                data: {
                    page: this.params.page,
                    status: type,
                },
                success: (res) => {
                    uni.stopPullDownRefresh();
                    this.status = "more";
                    this.goodsData = this.reload ? res.res.data.data : this.goodsData.concat(res.res.data.data); //数据 追加
                    this.params.page = res.res.data.current_page; //当前页码
                    this.last_page = res.res.data.last_page; //总页码
                },
            });
        },
        //获取商城订单列表
        async getShopList(type) {
            await uni.request({
                url: "/shop.shop/ShopOrderNoun",
                method: "POST",
                data: {
                    page: this.params.page,
                    status: type,
                },
                success: (res) => {
                    uni.stopPullDownRefresh();
                    this.status = "more";
                    this.goodsData = this.reload ? res.data.data : this.goodsData.concat(res.data.data); //数据 追加
                    this.params.page = res.data.current_page; //当前页码
                    this.last_page = res.data.last_page; //总页码
                    console.log(this.goodsData), "----------------------";
                },
            });
        },
        //顶部tab点击
        tabClick(index) {
            this.goodsData = [];
            this.params.page = 1;
            this.tabCurrentIndex = index;
            if (index == 0) {
                this.goodsType = "score";
                this.getScoreList(0);
            } else if (index == 1) {
                this.goodsType = "shop";
                this.getShopList(0);
            }
        },
        tabClick2(type) {
            this.goodsData = [];
            this.params.page = 1;
            this.tabCurrentIndex2 = type;
            if (this.tabCurrentIndex == 0) {
                this.getScoreList(type);
            } else if (this.tabCurrentIndex == 1) {
                this.getShopList(type);
            }
        },
        //切换tabbar
        changeTab(e) {
            this.currentTab = e;
            if (e == 0) {
                uni.redirectTo({ url: "/pages/merchant/goodsManagement" });
            } else if (e == 2) {
                uni.redirectTo({ url: "/pages/merchant/mine" });
            }
        },
    },
};
</script>
<style lang="scss" scoped></style>
